package FindManager;

import java.io.FileNotFoundException;
import java.util.List;

import database.query.Query;
import user.User;
import user.UserIF;
import user.blog.BlogIF;
import user.profile.Flags;
import user.profile.Profile;
import user.blog.post.Post;
import user.blog.post.comments.Comment;
/**
 * 
 * @author diego
 *
 */
public class FindBlog {
	private FindProfile findProfiles;
	
	public FindBlog(){
		findProfiles = new FindProfile();
	}
	
	public List<BlogIF> getListOfBlogsByName(String name) {
		List<BlogIF> blogs = null;
		try {
			blogs =  Query.findBlogs("titulo", name);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return blogs;
	}
	
	public String findBlogByName(String name){
		String blogs ="[]";
		try {
			blogs =  Query.findBlogs("titulo", name).toString();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return blogs;
	}
	
	public FindProfile getFindProfile(){
		return findProfiles;
	}
	
	public int getNumberOfComments(String login, String blogIdSession){
		int comments = 0;

		List<Post> posts;
		try {
			posts = Query.findPosts("blogID",blogIdSession );
			UserIF user = Query.findUser("login", login);			
			
			System.out.println("POSTS = "+posts);
			
			if((posts != null) && (user != null))
				for(Post post : Query.findPosts("blogID",blogIdSession )){

					for(Comment comment: post.getComments().getComments()){
						System.out.println("SessionIDComment = "+comment.getComment().getProperty("SESSIONID")+
								" = "+user.getSessionID());
						
						if(comment.getComment().getProperty("SESSIONID").equals(user.getSessionID()))
							comments +=1;
						
					}
				}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		




		return comments;
	}
	
	/*public static void main(String args[]){
		FindBlog FB = new FindBlog();
		Profile p1;
		User u;
		try {
			 p1 = new Profile("yuyu", "hakusho", "YUYU Hakusho", "yukuke@tokyo.com",
					"Masculino", "10/10/1984", "Rua ali", "Fisics", "ldkjshf",
					"Start Trek", "No musics", "kjdhfk");
			 
			 u = new User("yuyu", "hakusho", p1);
			 u.save();
				
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		System.out.println(FB.findProfiles.find(Flags.LOGIN, "yuyu"));
		System.out.println(FB.findProfiles.find(Flags.LOGIN, "uy"));
	}*/
	
	

}